/**
 * Created by Killer on 2016/1/2.
 */
$(document).ready(function () {

    //检测是否已经登陆，防止重复登陆
    $.ajax({
        url: "/fallwings/login/checkLogin",
        type: "get",
        statusCode: {
            200: function () {
                window.location.href = "index.html";
            },
            401: function () {
                initialization();
            }
        }
    });
    initialization();
});
function initialization() {
    $.backstretch([
        "/fallwings/static/login/img/2.jpg"
        , "/fallwings/static/login/img/3.jpg"
        , "/fallwings/static/login/img/1.jpg"
    ], {duration: 3000, fade: 750});

    /*
     Form validation
     */
    $('.login-form input[type="text"], .login-form input[type="password"], .login-form textarea').on('focus', function () {
        $(this).removeClass('input-error');
    });

    $('.login-form').on('submit', function (e) {

        $(this).find('input[type="text"], input[type="password"], textarea').each(function () {
            if ($(this).val() == "") {
                e.preventDefault();
                $(this).addClass('input-error');
                $('#form-username').popover('destroy');
                $('#form-password').popover('destroy');
            }
            else {
                $(this).removeClass('input-error');
                $('#form-username').popover('destroy');
                $('#form-password').popover('destroy');
            }
        });

    });



    //表单提交
    $("#login").click(function () {
        $.ajax({
            url: "/fallwings/login",
            type:"post",
            data: {
                userId: $("#form-username").val(),
                password: $("#form-password").val()
            },
            statusCode: {
                200: function () {
                    window.location.href = "/fallwings/index.html";
                },
                401: function () {

                    $('#form-username').popover('show');
                    $('#form-password').popover('show');
                }
            }
        })
    });
}
